package figure8.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import figure8.Controller;
import figure8.MIDITrack;
import figure8.Track;
import figure8.WAVTrack;

public class MIDITrackSettingsPanel extends JPanel {
	
	private JLabel label;
	
	public MIDITrackSettingsPanel(final Controller controller, final MIDITrack track) {
		super();
		//I know this is super ugly at the moment but I just want to get the instrument selector working first! 
		//this.setLayout(new BorderLayout());
		GridLayout gridy = new GridLayout (0, 1);
		GridLayout grid = new GridLayout (0, 2);
		this.setLayout(gridy);
		this.setPreferredSize(new Dimension(230, 150));
		
		label = new JLabel(track.getName(), JLabel.CENTER);
		this.add(label);
		JPanel mutingPanel = new JPanel();
		mutingPanel.setLayout(grid);
		JPanel volumePanel = new JPanel();
		volumePanel.setLayout(grid);
		//mutingPanel.setLayout(new BoxLayout(mutingPanel));
		final JToggleButton mute = new JToggleButton("M");
		mutingPanel.add(mute);
		
		final JButton delete = new JButton("D");
		mutingPanel.add(delete);
		
		delete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.deleteTrack(track);
			}
		});
		
		final JButton instrument = new JButton("Sound");
		mutingPanel.add(instrument);
		final MIDITrackSettingsPanel p = this;
		instrument.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				MIDISelectInstrumentWindow selectInstrument = new MIDISelectInstrumentWindow(controller.getWindow(), track.getInstruments(), track, p);
			}
		});
		
		this.add(mutingPanel);
		
		mute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				track.setMuted(!track.getMuted());
			}
		});
		//volumePanel.setLayout(new BoxLayout(volumePanel, BoxLayout.Y_AXIS));
		
		//volumePanel.add(new JLabel("Volume"));
		final JSlider volume = new JSlider(-30, 14, (int) track.getVolume());
		volumePanel.add(volume);
		volumePanel.add(instrument);
		this.add(mutingPanel);
		//this.add(new JLabel("Volume", JLabel.RIGHT));
		this.add(volumePanel);
		
		
		volume.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent arg0) {
				track.setVolume(volume.getValue());
			}
		});
	}
	
	public void set_label(String l){
		label.setText(l); 
	}
	
	public MIDITrackSettingsPanel getpanel(){
		return this;
	}
}
